package com.julis.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import com.Constant;
import com.baseutils.SpfUtils;
import com.julis.R;
import com.julis.activity.main.fragment.Function_Fragment;
import com.julis.activity.main.fragment.Home_Fragment;
import com.julis.activity.main.fragment.Record_Fragment;
import com.julis.activity.setting.Setting2_Activity;
import com.julis.activity.setting.Setting_Activity;
import com.julis.db.Z_UserInfoDao;
import com.julis.init.Init_HF;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class MainActivity extends FragmentActivity {

    AlertDialog.Builder alert;
    @BindView(R.id.names)
    TextView names;
    // 定义FragmentTabHost对象
    private FragmentTabHost mTabHost;
    // 定义一个布局
    private LayoutInflater layoutInflater;
    Timer timer = new Timer();
    String name;
    private static Boolean isQuit = false;
    public static Activity ActivityA;
    Z_UserInfoDao z_userInfoDao;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maintab);
        ButterKnife.bind(this);
        ActivityA = this;
        Init_HF.init(MainActivity.this);
        name = (String) SpfUtils.get(this, "name", "");
        String name = (String) SpfUtils.get(MainActivity.this, "name", "");
        names.setText("欢迎使用," + name);
        z_userInfoDao = new Z_UserInfoDao(MainActivity.this);

        initView2();


    }


    List<Class> fragmentArray = new ArrayList<Class>();
    List<Integer> mImageViewArray = new ArrayList<Integer>();
    List<String> mTextviewArray = new ArrayList<String>();

    private void initView2() {
        boolean Menu_Liushui = (boolean) SpfUtils.get(this, Constant.Menu_Liushui, true);
        boolean Menu_Recond = (boolean) SpfUtils.get(this, Constant.Menu_Recond, true);
        boolean Menu_SheBei = (boolean) SpfUtils.get(this, Constant.Menu_SheBei, false);
        // 实例化布局对象
        layoutInflater = LayoutInflater.from(this);
        // 实例化TabHost对象，得到TabHost
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        mTabHost.getTabWidget().setDividerDrawable(null);
        if (Menu_Liushui) {
            fragmentArray.add(Home_Fragment.class);
            mImageViewArray.add(R.drawable.tab_sy_btn);
            mTextviewArray.add("流水线检测");
        }
        if (Menu_Recond) {
            fragmentArray.add(Record_Fragment.class);
            mImageViewArray.add(R.drawable.tab_tk_btn);
            mTextviewArray.add("检测记录");
        }
        if (Menu_SheBei) {
            fragmentArray.add(Function_Fragment.class);
            mImageViewArray.add(R.drawable.tab_tk_btn);
            mTextviewArray.add("设备功能");
        }


        // 得到fragment的个数
        int count = fragmentArray.size();

        for (int i = 0; i < count; i++) {
            // 为每一个Tab按钮设置图标、文字和内容
            TabHost.TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray.get(i))
                    .setIndicator(getTabItemView(i));
            // 将Tab按钮添加进Tab选项卡中
            mTabHost.addTab(tabSpec, fragmentArray.get(i), null);
            // 设置Tab按钮的背景
            mTabHost.getTabWidget().getChildAt(i)
                    .setBackgroundResource(R.color.white);
        }


    }


    /**
     * 给Tab按钮设置图标和文字
     */
    private View getTabItemView(int index) {
        View view = layoutInflater.inflate(R.layout.tab_item_1_view, null);

        ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
        imageView.setImageResource(mImageViewArray.get(index));

        TextView textView = (TextView) view.findViewById(R.id.textview);
        textView.setText(mTextviewArray.get(index));

        return view;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (isQuit == false) {
                isQuit = true;
                Toast.makeText(getBaseContext(), "再按一次返回键退出程序",
                        Toast.LENGTH_SHORT).show();
                TimerTask task = null;
                task = new TimerTask() {
                    public void run() {
                        isQuit = false;
                    }
                };
                timer.schedule(task, 2000);
            } else {

                finish();

            }
        }
        return false;
    }

    @OnClick(R.id.setting)
    public void onViewClicked() {
        String name = (String) SpfUtils.get(MainActivity.this, "name", "");
        if (!name.equals("admin")) {
            startActivity(new Intent(MainActivity.this, Setting2_Activity.class));
        } else {
            startActivity(new Intent(MainActivity.this, Setting_Activity.class));
        }
    }
}
